package com.mmc.service.email;

import java.io.UnsupportedEncodingException;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.mmc.domain.email.MailBean;

/**
 * 这个类是发送邮件的工具类
 * @author lifeng
 *
 */
@Service
public class EmailServiceAPI {
	@Autowired
	private JavaMailSenderImpl javaMailSenderImpl;
	
	public MimeMessage createMimeMessage() throws MessagingException, UnsupportedEncodingException{
		MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
		return mimeMessage;
	}
	
	public void sendMail(MailBean mailBean) throws UnsupportedEncodingException, MessagingException{
		mailBean.setFrom("lifeng_it@163.com");
		MimeMessage mimeMessage = createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
		messageHelper.setFrom(mailBean.getFrom(), mailBean.getFromName()); 
		messageHelper.setSubject(mailBean.getSubject());  
		messageHelper.setTo(mailBean.getToEmails());  
		messageHelper.setText(mailBean.getContext(), true); // html: true 
		javaMailSenderImpl.send(mimeMessage);
	}
}
